//
//  XGBaseSettingViewController.swift
//  TodayNews
//
//  Created by monkey on 2020/2/9.
//  Copyright © 2020 itcast. All rights reserved.
//

import UIKit
import SwiftTheme

class XGBaseSettingViewController: UITableViewController
{
    // MARK: - 开放方法
    
    public init(plistName: String)
    {
        self.plistName = plistName
        
        super.init(style: .plain)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// 更新数据模型
    /// - Parameters:
    ///   - indexPath: 索引
    ///   - itemModel: 数据模型
    final public func updateItemModel(indexPath: IndexPath,itemModel: XGSettingItemModel) -> Void
    {
        itemSetions?[indexPath.section][indexPath.row] = itemModel
        tableView.reloadRows(at: [indexPath], with: .fade)
    }
    
    /// 获取数据模型
    /// - Parameter indexPath: 索引
    final public func itemModelWithIndexPath(_ indexPath: IndexPath) -> XGSettingItemModel?
    {
        return itemSetions?[indexPath.section][indexPath.row]
    }
    
    // MARK: - 控制器生命周期方法
    
    override func viewDidLoad()
    {
        super.viewDidLoad()
        
        // 设置tableView
        setUpTableView()
        // 加载数据
        loadData()
    }
    
    /// 配置文件名称
    private let plistName: String
    /// 数据模型
    private var itemSetions: [[XGSettingItemModel]]?
}

// MARK: - 数据源和代理方法

extension XGBaseSettingViewController
{
    override func numberOfSections(in tableView: UITableView) -> Int
    {
        return itemSetions?.count ?? 0
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return itemSetions?[section].count ?? 0
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell = tableView.xg_dequeueReusableCell(indexPath: indexPath) as XGSettingTableViewCell
        let itemModel = itemSetions?[indexPath.section][indexPath.row]
        cell.itemModel = itemModel
        return cell
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
    {
        let itemModel = itemSetions?[indexPath.section][indexPath.row]
        return (itemModel?.subTitle != nil ? 44 + 20 : 44)
    }
    
    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
    {
        let view = tableView.xg_dequeueReusableHeaderFooterView() as XGMineTableHeaderFooterView
        return view
    }
}

// MARK: - 私有扩展

private extension XGBaseSettingViewController
{
    /// 设置tableView
    func setUpTableView() -> Void
    {
        tableView.xg_registerCell(cellType: XGSettingTableViewCell.self)
        tableView.xg_registerHeaderFooterView(viewType: XGMineTableHeaderFooterView.self)
        tableView.sectionHeaderHeight = 10
        tableView.tableFooterView = UIView()
        tableView.theme_backgroundColor = "colors.cell_background"
        tableView.theme_separatorColor = "colors.cell_separator"
    }
    
    /// 加载数据
    func loadData() -> Void
    {
        guard let path = Bundle.main.path(forResource: plistName, ofType: nil),
              let sectionsArr = NSArray(contentsOfFile: path)  else {
                return
        }
        
        itemSetions = sectionsArr.compactMap { (dictArr) -> [XGSettingItemModel]? in
            (dictArr as? [[String: Any]])?.compactMap({ (dict) -> XGSettingItemModel? in
                return XGSettingItemModel.deserialize(from: dict)
            })
        }
    }
}
